package com.dwxt.community.admin.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.community.dao.TrashIntegralRecoadMapper;
import com.dwxt.community.entity.Information;
import com.dwxt.community.entity.TrashIntegralRecoad;
import com.dwxt.community.entity.TrashIntegralRecoadExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TrashServiceAdmin {

    @Autowired
    private TrashIntegralRecoadMapper trashIntegralRecoadMapper;

    public BaseResult deliverTimeStatistics() {
        List<String> xList = new ArrayList<>();
        List<Integer> yList = new ArrayList<>();
        for (int i = 2; i <= 24; i = i + 2) {
            xList.add(i + "h");
            Integer count = trashIntegralRecoadMapper.selectTrashTimeCount(i);
            yList.add(count);
        }
        Map<String, List> map = new HashMap<>();
        map.put("x", xList);
        map.put("y", yList);
        return BaseResult.ok(map);
    }

    public BaseResult numberStatistics() {
        Map<String, Integer> statistics = new HashMap<>();
        statistics.put("可回收物", 0);
        statistics.put("有害垃圾", 1);
        statistics.put("厨余垃圾", 2);
        statistics.put("其他垃圾", 3);

        for (Map.Entry<String, Integer> entry : statistics.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            Integer number = trashIntegralRecoadMapper.selectTrashNumberCount(value);

            statistics.put(key, number);
        }

        List<Map> list = new ArrayList<>();
        for (Map.Entry<String, Integer> stringIntegerEntry : statistics.entrySet()) {
            Map<String, Object> map = new HashMap<>();
            String key = stringIntegerEntry.getKey();
            Integer value = stringIntegerEntry.getValue();
            map.put("name", key);
            map.put("value", value);
            list.add(map);
        }

        return BaseResult.ok(list);
    }

    public BaseResult deliverRateStatistics() {
        List<String> xList = new ArrayList<>();
        xList.add("10次以下");
        xList.add("10-19");
        xList.add("20-29");
        xList.add("30-39");
        xList.add("40以上");

        List<Integer> yList = new ArrayList<>();
        for (int i = 0; i <= 40; i = i + 10) {
            Integer count;
            if (i != 40) {
                count = trashIntegralRecoadMapper.selectRateCount(i, i + 10);
            } else {
                count = trashIntegralRecoadMapper.selectRateCount(i, null);
            }
            yList.add(count);
        }
        Map<String, List> map = new HashMap<>();
        map.put("x", xList);
        map.put("y", yList);
        return BaseResult.ok(map);
    }

    public Long countTrash(String id) {
        TrashIntegralRecoadExample example = new TrashIntegralRecoadExample();
        example.createCriteria().andDelivererIdEqualTo(id);
        return Long.parseLong(trashIntegralRecoadMapper.countByExample(example) + "");
    }
}
